/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tree;

import java.util.List;

/**
 *
 * @author Ej-Dý
 */
public class CastForTree extends VlastniProgramTree {

    private PrirazeniTree v1;
    private BinaryTree p;
    private PrirazeniTree v2;
    private List<VlastniProgramTree> prog;

    public CastForTree(Position start, Position end, PrirazeniTree v1, BinaryTree p, PrirazeniTree v2, List<VlastniProgramTree> prog) {
        super(start, end);
        this.v1 = v1;
        this.p = p;
        this.v2 = v2;
        this.prog = prog;
    }

    public PrirazeniTree getVyraz1() {
        return v1;
    }

    public PrirazeniTree getVyraz2() {
        return v2;
    }

    public BinaryTree getPorovnani() {
        return p;
    }

    public List<VlastniProgramTree> getBody() {
        return prog;
    }

    @Override
    public void accept(TreeVisitor visitor) {
        visitor.visitFor(this);
    }

    @Override
    public void generate(Context ctx) {
        v1.generate(ctx);
        String lab1 = ctx.nextLabel();
        String lab2 = ctx.nextLabel();
        ctx.addAttr("lab", lab2);
        ctx.println("label " + lab1 + ":");

        p.generate(ctx);
        ctx.removeAttr("lab");
        for (VlastniProgramTree vt : prog) {
            vt.generate(ctx);
        }
        v2.generate(ctx);
        ctx.println("goto " + lab1);
        ctx.println("label " + lab2 + ":");


    }

    @Override
    public String toString() {
        return String.format("For %s: [%s, %s, %s, %s]", super.toString(), v1, p, v2, prog);
    }
}
